package javango.forms;

import java.util.List;
import java.util.Map;

import javango.forms.fields.FieldFactory;
import javango.forms.fields.IntegerField;
import javango.forms.fields.LongField;

import com.google.inject.Inject;
import com.google.inject.Provider;

public class FormsetFactory<T extends Form> {

	protected final Class<T> formClass;
	protected final Integer extra;
	protected final String prefix;
	protected final Map<String, String[]> data;
	protected final boolean canDelete;
	
	protected final List<?> initial;
	
	protected final Forms forms;
	protected final FieldFactory fieldFactory;
	
	@Inject
	public FormsetFactory(Forms forms, FieldFactory fieldFactory) {
		super();
		this.forms = forms;
		this.fieldFactory = fieldFactory;
		this.formClass = null;
		this.extra = null;
		this.initial = null;
		this.prefix = null;
		this.data = null;
		this.canDelete = false;
	}

	private FormsetFactory(Forms forms, FieldFactory fieldFactory, Class<T> formClass, Integer extra,  Map<String, String[]> data, List<?> initialList, String prefix, boolean canDelete) {
		super();
		this.forms = forms;
		this.fieldFactory = fieldFactory;
		this.formClass = formClass;
		this.extra = extra;
		this.initial = initialList;
		this.prefix = prefix;
		this.data = data;
		this.canDelete = canDelete;
	}

	public <F extends Form> FormsetFactory<F> setFormClass(Class<F> formClass) {
		return new FormsetFactory<F>(forms, fieldFactory, formClass, this.extra, this.data, this.initial, this.prefix, this.canDelete);
	}

	public FormSet<T> build() throws ValidationException {
		return new FormSet<T>(forms, fieldFactory, formClass, canDelete, initial, data, prefix, extra);
	}

	public FormsetFactory<T> setExtra(Integer extra) {
		return new FormsetFactory<T>(forms, fieldFactory, this.formClass, extra, this.data, this.initial, this.prefix, this.canDelete);
	}

	public FormsetFactory<T> setInitial(List<?> initialList) {
		return new FormsetFactory<T>(forms, fieldFactory, this.formClass, this.extra, this.data, initialList, this.prefix, this.canDelete);
	}

	public FormsetFactory<T> setData(Map<String, String[]> data) {
		return new FormsetFactory<T>(forms, fieldFactory, this.formClass, this.extra, data, this.initial, this.prefix, this.canDelete);
	}

	public FormsetFactory<T> setPrefix(String prefix) {
		return new FormsetFactory<T>(forms, fieldFactory, this.formClass, this.extra, this.data, this.initial, prefix, this.canDelete);
	}
	
	public FormsetFactory<T> setCanDelete(boolean canDelete) {
		return new FormsetFactory<T>(forms, fieldFactory, this.formClass, this.extra, this.data, this.initial, this.prefix, canDelete);
	}	
}
